import { registerClass } from "../../../cfw/tools/class_decorator";
import { SDKSystem } from "../../core/system/SDKSystem";
import SDKHelper from "../../tools/SDKHelper";
import { ResultCallback, ResultState, SDKStyle } from "../../SDKDefine";


@registerClass('WXSystem')
export class WXSystem extends SDKSystem {

    protected infoButton: any;
    hasShare(): boolean {
        return true;
    }

    vibrateShort() {
        this.sdk.vibrateShort();
    }
    getScreenSize(): { width: number, height: number } {
        let systemInfo = this.sdk.getSystemInfoSync();
        return { width: systemInfo.screenWidth, height: systemInfo.screenHeight }
    }
    compareVersion(bVersion: string): number {
        const version = this.sdk.getSystemInfoSync().SDKVersion

        let flag = SDKHelper.compareVersion(version, bVersion)

        console.log("Banner 版本: ", version, ' bVersion ', bVersion, ' flag ', flag)
        return flag
    }
    //展示网络图片
    previewImage(imgUrl: string) {
        this.sdk.previewImage({
            current: imgUrl, // 当前显示图片的http链接
            urls: [imgUrl] // 需要预览的图片http链接列表
        })
    }

    //跳转能力
    navigateToMiniProgram(appID: string) {
        this.sdk.navigateToMiniProgram({
            appId: appID,
            success: () => {

            }
        })
    }
    showToast(title: string) {
        this.sdk.showToast({ title: title })
    }

    postMessage(msg: any) {
        let context = this.sdk.getOpenDataContext()
        if (context) {
            msg.channelID = this.data.name;
            context.postMessage(msg)
        }
    }

    checkForUpdate() {

        const updateManager = this.sdk.getUpdateManager()
        if (!updateManager) {
            return
        }
        updateManager.onCheckForUpdate(function (res: any) {
            // 请求完新版本信息的回调
            // console.log(res.hasUpdate)
        })

        updateManager.onUpdateReady(() => {
            this.sdk.showModal({
                title: '更新提示',
                content: '新版本已经准备好，是否重启应用？',
                success: (res: any) => {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate()
                    }
                }
            })
        })

        updateManager.onUpdateFailed(() => {
            // 新版本下载失败
            this.showToast('新版本下载失败,下次启动继续...')
        })

    }

    createGameClubButton(param: { icon: string, style: SDKStyle, openlink: string }) {
        this.sdk.createGameClubButton({
            icon: param.icon,
            style: param.style,
            openlink: param.openlink,
        })
    }

    authorize(obj: { scope: string, callback: ResultCallback }): void {
        this.sdk.authorize({
            scope: obj.scope,
            success() {
                console.log('authorize success ')
                // 用户已经同意保存到相册功能，后续调(用 wx.saveImageToPhotosAlbum 接口不会弹窗询问
                obj.callback(ResultState.YES)
            },
            fail(err) {
                console.log('authorize fail ', err)
                obj.callback(ResultState.NO)
            },
            complete() {
                console.log('getSetting complete ')
            }
        })
    }

    setUserCloudStorage(param: { key: string, value: any, callback: ResultCallback }): void {
        this.sdk.setUserCloudStorage({ //调用微信接口上报关卡等级信息，用于好友圈排行
            KVDataList: [
                { key: param.key, value: `${param.value}` }
            ],

            success: () => {
                param.callback(ResultState.YES)
            },

            fail: (err: any) => {
                console.log('setUserCloudStorage err ', err)
                param.callback(ResultState.NO)
            }
        })
    }

    createInfoButton(param: { type: string, text: string, image: string, style: SDKStyle, callback: (r: ResultState, data?: any) => void }): void {
        let sdk = this.sdk;

        console.log(' style ', param.style)
        this.changeStyle(param.style)
        console.log(' style ', param.style)
        // 否则，先通过 wx.createUserInfoButton 接口发起授权
        let button = sdk.createUserInfoButton({
            type: param.type,
            text: param.text,
            image: param.image,
            style: param.style
        })
        button.onTap((res: any) => {
            // 用户同意授权后回调，通过回调可获取用户头像昵称信息
            console.log('createInfoButton onTap ', res)
            param.callback(ResultState.YES, res)
        })
        this.infoButton = button;
    }

    destroyInfoButton(): void {
        if (this.infoButton) {
            this.infoButton.destroy();
            this.infoButton = null;
        }
    }
    setInfoButtonVisible(flag: boolean) {
        if (this.infoButton) {
            if (flag) {
                this.infoButton.show()
            } else {
                this.infoButton.hide();
            }
        }

    }
    getUserInfo(param: { callback: (r: ResultState, res: any) => void }): void {
        // 通过 wx.getSetting 查询用户是否已授权头像昵称信息
        let sdk = this.sdk;
        sdk.getUserInfo({
            success: function (res: any) {
                console.log(res.userInfo)
                param.callback(ResultState.YES, res)
            },
            fail: () => {
                param.callback(ResultState.NO, null)
            }
        })
    }

    //获得授权结果
    getSetting(obj: { scope: string, callback: ResultCallback }): void {
        console.log(' obj.scope ', obj.scope)
        this.sdk.getSetting({
            success: (res: any) => {
                console.log(' res ', res)
                let data = res.authSetting[obj.scope];
                console.log(' data ', data)
                if (!data) {
                    obj.callback(ResultState.NO)
                } else {
                    obj.callback(ResultState.YES)
                }
            },
            fail: () => {
                obj.callback(ResultState.NO)
            }
        })
    }


    requestSubscribeSystemMessage() {
        // * 需要先使用 compareVersion 判断接口是否可用
        const sdkVersion = this.sdk.getSystemInfoSync().SDKVersion
        if (SDKHelper.compareVersion(sdkVersion, '2.32.1') >= 0) {
            this.sdk.requestSubscribeSystemMessage({
                msgTypeList: ['SYS_MSG_TYPE_WHATS_NEW'],
                success(res) {
                    console.log(res)
                }
            });
        }
    }
}